package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.CartVO;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.util.CookieUtils;
import io.netty.util.Timeout;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("cartController")
public class CartController {

    @Reference
    private CartService cartService;

    //获取购物车信息
    @RequestMapping("getCart")
    @ResponseBody
    public List<CartVO> getCart(HttpServletRequest request,HttpServletResponse response){
        //获取登录用户
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        //如果用户未登录 从Cookie 中获取
        String cartList = CookieUtils.getCookieValue(request, "cartList", true);
        if (StringUtils.isBlank(cartList)){
            cartList = "[]";
        }
        //使用工具类  将JSON转成对象
        List<CartVO> cartListCookie = JSONObject.parseArray(cartList, CartVO.class);
        if (tbUser == null){

            return cartListCookie;
        }else{//如果用户已经登录从redis 中获取
            List<CartVO> listCartFromRedis = cartService.findListCartFromRedis(tbUser.getUsername());
            //判断Cookie 中是否有数据
            if (cartListCookie.size() > 0){
                //合并购物车
                List<CartVO> merageCartList = cartService.merageCartList(cartListCookie, listCartFromRedis);
                //将合并过后的放入redis
                cartService.addCartListToRedis(tbUser.getUsername(),merageCartList);
                //清空cookie 数据
                CookieUtils.deleteCookie(request,response,"cartList");
                return merageCartList;
            }
            return listCartFromRedis;
        }

    }

    //Cookie 就是一个key - vvalue 结构 添加 删除 查询 没有修改  ，修改就是覆盖
    @RequestMapping("addCookie")
    @ResponseBody
    public String addCookie(HttpServletRequest request, HttpServletResponse response,Long itemId,Integer num){

        List<CartVO> cartList = getCart(request,response);
        //调用购物车接口添加购物车数据
        cartList = cartService.insertCart(cartList,itemId,num);
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (tbUser == null){//未登录
            String cartListJson = JSONObject.toJSONString(cartList);
            CookieUtils.setCookie(request,response,"cartList",cartListJson,true);
            System.out.println(cartListJson);
        }else{//已登录
            cartService.addCartListToRedis(tbUser.getUsername(),cartList);
        }


        return "ok";
    }

    //Cookie  值以 String 类型存放
    @RequestMapping("getCookieValue")
    @ResponseBody
    public String getCookieValue(HttpServletRequest request){

        String cookieValue = CookieUtils.getCookieValue(request, "myCookie", true);

        return cookieValue;
    }

    @RequestMapping("deleteCookie")
    @ResponseBody
    public String deleteCookie(HttpServletRequest request,HttpServletResponse response){

        CookieUtils.deleteCookie(request,response,"myCookie");
        return "ok";

    }



}
